// components/user-data-base/user-data-base.js
import util from '../../utils/util'
import actions from '../../datas/actions'
const userDao = require('../../dao/userDao')
const app = getApp()
import {
  areaList
} from '@vant/area-data';
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    from: '新增资料' //新增资料，修改资料

  },

  /**
   * 组件的初始数据
   */
  data: {
    areaList,
    //滚动距离
    scrollY: 100,
    show: false,
    dateShow: false,
    areaShow: false,
    currentDate: new Date().getTime(),
    minDate: new Date().setFullYear((new Date().getFullYear() - 60)),
    maxDate: new Date().setFullYear((new Date().getFullYear() - 16)),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    title: "",
    actions: [],
    user: {
      oneself: '本人发布',
      sex: 1,
      age: 100,
      birthday: "请选择",
      height: "请选择",
      education: '请选择',
      marriage: "请选择",
      work: "请选择",
      income: "请选择",
      hometown: 110101,
      hometownName: "请选择",
      currentHome: 110101,
      currentHomeName: "请选择",
    }
    
  },

  lifetimes: {
    ready() {
      let systemInfo = wx.getSystemInfoSync()
      let pxToRpxScale = 750 / systemInfo.windowWidth;
      if(app.data.currentUser) {
        this.setData({
          //减去上面的tabs标题栏，减去下面的按钮高度
          scrollY: systemInfo.windowHeight * pxToRpxScale - 100 - 100,
          'user.hometownName': app.data.currentUser.hometownName,
          'user.hometown': app.data.currentUser.hometown,
          'user.currentHomeName': app.data.currentUser.currentHomeName,
            'user.currentHome': app.data.currentUser.currentHome,
            'user.birthday': app.data.currentUser.birthday,
        'user.age': app.data.currentUser.age,
        'user.oneself': app.data.currentUser.oneself,
        'user.sex': app.data.currentUser.sex,
        'user.height': app.data.currentUser.height,
        'user.education': app.data.currentUser.education,
        'user.marriage': app.data.currentUser.marriage,
        'user.work': app.data.currentUser.work,
        'user.income': app.data.currentUser.income
        })
      } else {
        this.setData({
          //减去上面的tabs标题栏，减去下面的按钮高度
          scrollY: systemInfo.windowHeight * pxToRpxScale - 100 - 100,
        })
      }
     
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    
    onCancel() {
      this.setData({
        areaShow: false,
        dateShow: false,
        show: false
      });
    },
    //地址选择
    onAreaConfirm(event) {
      let title = event.currentTarget.dataset.title
      let values = event.detail.values
      let name = values[0].name + '-' + values[1].name + '-' + values[2].name
      let code = values[2].code
      console.log(name)
      switch (title) {
        case "户籍地":
          this.setData({
            areaShow: false,
            'user.hometownName': name,
            'user.hometown': code,
          });
          break
        case "常住地":
          this.setData({
            areaShow: false,
            'user.currentHomeName': name,
            'user.currentHome': code,
          });
          break
      }
      console.log(this.data.user)
    },

    //生日 选择
    onDateTimeConfirm(event) {
      let date = new Date(event.detail)
      this.setData({
        dateShow: false,
        currentDate: event.detail,
        'user.birthday': util.getDate(date),
        'user.age': new Date().getFullYear() - date.getFullYear()
      });
    },

  

    //van-action-sheet
    onClose() {
      this.setData({
        show: false
      });
    },

    bindOk() {
      if (this.data.user.age == 100 || '请选择' == this.data.user.birthday || '请选择' == this.data.user.height ||
        '请选择' == this.data.user.education || '请选择' == this.data.user.education ||
        '请选择' == this.data.user.work || '请选择' == this.data.user.income ||
        '请选择' == this.data.user.hometownName || '请选择' == this.data.user.currentHomeName) {
        wx.showToast({
          title: '请完善基础信息',
          icon: 'none'
        })
        return
      }
      var user = this.data.user
      console.log(user)
      user.name = app.data.userInfo.nickName
      user.updateTime = new Date()
      if("新增资料" == this.data.from) {
        wx.showLoading()
        user.createDate = new Date()
        user.avatarUrl = this.data.sex == 1 ? "../../assets/user-man.png" : "../../assets/user-woman.png"
        user.introduction = "我还没输入个性介绍~"
        user.sexIntroduction = "我还没输入择偶标准~"
        user.new = true
        user.like = []
        user.likeMe = []
        userDao.add(user).then(res => {
          wx.hideLoading()
          console.log(res)
          user._id = res._id
          app.data.currentUser = user
          util.setCache(app.keys.currentUser, user)
          wx.showToast({
            title: '新增成功',
          })
        })
      } else {
        userDao.update(app.data.currentUser._id, user).then(res => {
          app.data.currentUser.hometownName = user.hometownName
          app.data.currentUser.hometown = user.hometown
          app.data.currentUser.currentHomeName = user.currentHomeName
          app.data.currentUser.currentHome = user.currentHome
          app.data.currentUser.birthday = user.birthday
          app.data.currentUser.age = user.age
          app.data.currentUser.oneself = user.oneself
          app.data.currentUser.sex= user.sex
          app.data.currentUser.height = user.height
          app.data.currentUser.education = user.education
          app.data.currentUser.marriage= user.marriage
          app.data.currentUser.work = user.work
          app.data.currentUser.incom = user.income
          util.setCache(app.keys.currentUser, app.data.currentUser)
          wx.showToast({
            title: '修改成功',
          })
        })
      }
     


    },

    //单元格点击事件
    cellClick(title, action) {
      this.setData({
        show: !this.data.show,
        title: title,
        actions: action
      })
    },
    bindCellClick(e) {
      let title = e.currentTarget.dataset.title
      switch (title) {
        case "关系":
          this.cellClick(title, actions.oneself_actions)
          break
        case "性别":
          this.cellClick(title, actions.sex_actions)
          break
        case "生日":
          this.setData({
            dateShow: !this.data.dateShow,
            title: title
          })
          break
        case "身高":
          this.cellClick(title, actions.height_actions)
          break
        case "学历":
          this.cellClick(title, actions.education_actions)
          break
        case "婚史":
          this.cellClick(title, actions.marriage_actions)
          break
        case "所在行业":
          this.cellClick(title, actions.work_actions)
          break
        case "月薪":
          this.cellClick(title, actions.income_actions)
          break
        case "户籍地":
          this.setData({
            areaShow: !this.data.areaShow,
            title: title
          })
          break
        case "常住地":
          this.setData({
            areaShow: !this.data.areaShow,
            title: title
          })
          break
      }
    },

    onSelect(event) {
      let name = event.detail.name
      switch (this.data.title) {
        case "关系":
          this.setData({
            'user.oneself': name
          })
          break
        case "性别":
          this.setData({
            'user.sex': name == '男' ? 1 : 0
          })
          break
        case "身高":
          this.setData({
            'user.height': name
          })
          break
        case "学历":
          this.setData({
            'user.education': name
          })
          break
        case "婚史":
          this.setData({
            'user.marriage': name
          })
          break
        case "所在行业":
          this.setData({
            'user.work': name
          })
          break
        case "月薪":
          this.setData({
            'user.income': name
          })
          break

      }

    },


  }
})